<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { selectRank } from '@/api/article';
import type { Article } from '@/api/article/types.ts';
import { Icon } from '@iconify/vue';
import Message from '@/components/message';

const articleList = ref<Article[]>([]);
const loading = ref(true);
const error = ref<string | null>(null);

const selectRankArticle = async () => {
  try {
    loading.value = true;
    error.value = null;
    const result = await selectRank();
    articleList.value = result.data;
  } catch (err) {
    error.value = '获取排行榜失败';
    Message.error('获取排行榜失败');
    console.error('获取点击排行失败:', err);
  } finally {
    loading.value = false;
  }
};

const formatDate = (date: string) => {
  const dateObj = new Date(date);
  if (isNaN(dateObj.getTime())) return '无效日期';
  return new Intl.DateTimeFormat('zh-CN', {
    month: '2-digit',
    day: '2-digit'
  }).format(dateObj);
};

const handleImageError = (e: Event) => {
  const target = e.target as HTMLImageElement;
  target.src = '../assets/images/fallback-image.jpg';
};

onMounted(selectRankArticle);
</script>

<template>
  <div class="hot-articles">
    <div class="header">
      <span class="title">本周热门</span>
    </div>

    <div v-if="loading" class="loading-state">
      <div class="loader"></div>
    </div>

    <div v-else-if="error" class="error-state">
      {{ error }}
      <button @click="selectRankArticle">重试</button>
    </div>

    <div v-else class="articles-container">
      <!-- 第一名文章 -->
      <router-link
        v-if="articleList[0]"
        class="featured-article"
        :to="`/detail/${articleList[0].slug}?id=${articleList[0].id}`"
      >
        <div class="featured-content">
          <div class="rank-badge">TOP 1</div>
          <img
            :src="articleList[0].coverImage"
            :alt="articleList[0].title"
            @error="handleImageError"
          />
          <div class="info-overlay">
            <h4>{{ articleList[0].title }}</h4>
            <div class="meta">
              <span class="views">
                <i>🔥</i>{{ articleList[0].views }}
              </span>
              <span class="date">
                <i>📅</i>{{ formatDate(articleList[0].publishTime) }}
              </span>
            </div>
          </div>
        </div>
      </router-link>

      <!-- 其他文章列表 -->
      <div class="articles-list">
        <router-link
          v-for="(item, index) in articleList.slice(1)"
          :key="item.id"
          class="article-item"
          :to="`/detail/${item.slug}?id=${item.id}`"
          target="_blank"
        >
          <span class="rank-number" :class="`rank-${index + 2}`">{{ index + 2 }}</span>
          <div class="item-content">
            <h5>{{ item.title }}</h5>
            <div class="item-meta">
              <span class="views">
                <i>🔥</i>{{ item.views }}
              </span>
              <span class="date">
                <i>📅</i>{{ formatDate(item.publishTime) }}
              </span>
            </div>
          </div>
        </router-link>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.hot-articles {
  background: #fff;
  border-radius: 12px;
  padding: 16px;

  .header {
    margin-bottom: 12px;

    .title {
      font-size: 15px;
      font-weight: 600;
      color: #1a1a1a;
      position: relative;
      padding-left: 10px;

      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 3px;
        height: 14px;
        background: #60a5fa;
        border-radius: 2px;
      }
    }
  }

  .loading-state {
    text-align: center;
    padding: 20px 0;

    .loader {
      width: 20px;
      height: 20px;
      border: 2px solid #f3f3f3;
      border-top: 2px solid rgba(236, 158, 68, 0.88);
      border-radius: 50%;
      margin: 0 auto;
      animation: spin 0.8s linear infinite;
    }
  }

  .error-state {
    text-align: center;
    color: #ff4d4f;
    padding: 16px 0;
    font-size: 13px;

    button {
      margin-left: 8px;
      padding: 4px 12px;
      border: 1px solid currentColor;
      border-radius: 4px;
      background: transparent;
      color: inherit;
      font-size: 12px;
      cursor: pointer;
      transition: all 0.2s ease;

      &:hover {
        background: #ff4d4f;
        color: #fff;
      }
    }
  }

  .articles-container {
    display: flex;
    flex-direction: column;
    gap: 12px;

    .featured-article {
      text-decoration: none;
      border-radius: 8px;
      overflow: hidden;
      position: relative;
      height: 140px; // 减小高度

      &:hover {
        .featured-content img {
          transform: scale(1.05);
        }
      }

      .featured-content {
        position: relative;
        height: 100%;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
          transition: transform 0.3s ease;
        }

        .rank-badge {
          position: absolute;
          top: 8px;
          left: 8px;
          background: rgba(255, 71, 87, 0.9);
          padding: 3px 8px;
          border-radius: 4px;
          font-size: 12px;
          font-weight: 500;
          color: #fff;
        }

        .info-overlay {
          position: absolute;
          bottom: 0;
          left: 0;
          right: 0;
          padding: 12px;
          background: linear-gradient(
            to top,
            rgba(0, 0, 0, 0.75),
            rgba(0, 0, 0, 0)
          );

          h4 {
            color: #fff;
            margin: 0 0 6px;
            font-size: 14px;
            font-weight: 500;
            line-height: 1.4;
          }

          .meta {
            display: flex;
            gap: 12px;
            color: rgba(255, 255, 255, 0.9);
            font-size: 12px;

            span {
              display: flex;
              align-items: center;
              gap: 4px;

              i {
                font-style: normal;
                font-size: 11px;
              }
            }
          }
        }
      }
    }

    .articles-list {
      display: flex;
      flex-direction: column;
      gap: 10px;

      .article-item {
        display: flex;
        align-items: flex-start;
        gap: 10px;
        padding: 8px;
        text-decoration: none;
        border-radius: 6px;
        transition: all 0.2s ease;

        &:hover {
          background: #f8f9fa;
          transform: translateX(4px);
        }

        .rank-number {
          width: 20px;
          height: 20px;
          display: flex;
          align-items: center;
          justify-content: center;
          border-radius: 4px;
          color: #fff;
          font-size: 12px;
          font-weight: 500;
          flex-shrink: 0;
        }

        .rank-2 { background: #ff9f43; }
        .rank-3 { background: #20bf6b; }
        .rank-4 { background: #4b7bec; }
        .rank-5 { background: #a55eea; }
        .rank-6, .rank-7, .rank-8, .rank-9, .rank-10 {
          background: #778ca3;
        }

        .item-content {
          flex: 1;
          min-width: 0;

          h5 {
            margin: 0 0 4px;
            font-size: 13px;
            font-weight: 500;
            color: #1a1a1a;
            line-height: 1.4;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
          }

          .item-meta {
            display: flex;
            gap: 12px;
            font-size: 12px;
            color: #666;

            span {
              display: flex;
              align-items: center;
              gap: 3px;

              i {
                font-style: normal;
                font-size: 11px;
              }
            }

            .views {
              color: #ff6b6b;
            }
          }
        }
      }
    }
  }
}
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>
